<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of cls_dynamic
 *
 * @author yjj
 */
class cls_dynamic {
//模版名
var $LibIDC;

public $imgname;


//配置文件的路径
function config_file(){
      return ROOT_PATH . '/sources/themes/' . $GLOBALS['_CFG']['template'].'/library/config.xml';
}
/**
 * 获得项目库xml配置的具体属性
 * @access  public
 * @param   array     LidIDC
 * @return  array
 * @author  杨敬君
 * @CreateTime 2009-7-30 15:54
 */
    function get_dynamic(){
    $arr = array();
    $doc = new DOMDocument();
    $doc->load($this->config_file());
    $libs = $doc->getElementsByTagName("lib");
	foreach( $libs as $lib_node){
		   // $arr[$slib_node][num]=$key->getAttribute("num");
		if($this->LibIDC==$lib_node->getAttribute("id")){
		  $slibs=$lib_node->getElementsByTagName("data");
	  	  foreach ($slibs as $slib_node =>$key){
    	    if($key->getAttribute("datatype")<>"5"){//非图文
    	    $sort=$key->getAttribute("Sort");
	  	  	$datatype=$key->getAttribute("datatype");
			$arr[$slib_node]['nodeid']=$slib_node;
	  	  	$arr[$slib_node]['datatype']=$this->get_typestring($datatype);
	  	  	$arr[$slib_node]['type']=$key->getAttribute("datatype");
	  	  	$arr[$slib_node]['sort']=$key->getAttribute("Sort");
	  	  	$arr[$slib_node]['libname']=$this->LibIDC;
	  	  	$arr[$slib_node]['target']=$key->getAttribute("target");
	  	  	$arr[$slib_node]['num']=$key->getAttribute("num");
	  	  	$arr[$slib_node]['height']=$key->getAttribute("height");//高度
	  	  	$arr[$slib_node]['width']=$key->getAttribute("width");//宽度
	  	  	$arr[$slib_node]['islink']=$key->getAttribute("islink");//是否链接1是0否
	  	  	$arr[$slib_node]['Sort']=$key->getAttribute("Sort");//排序
	  	  	$arr[$slib_node]['length']=$key->getAttribute("length");//文本框串长度
	  	  	if($datatype=="1" or $datatype=="2"){
	  	        $arr[$slib_node]['target']=$key->getAttribute("target");//除图片/flash/html/图文 外的链接目标
	  	  	 }
	  	    $arr[$slib_node]['dbdata']=$this->get_dynamicDBview($sort,$this->LibIDC,$datatype,$key->getAttribute("num"));
    	    }else{//图文
			     $arr[$slib_node]['nodeid']=$slib_node;
    	    	 $arr[$slib_node]['libname']=$this->LibIDC;
    	    	 $arr[$slib_node]['type']=$key->getAttribute("datatype");
                 $arr[$slib_node]['datatype']=$this->get_typestring(5);//get_typestring(5);
    	    	 $arr[$slib_node]['sort']=$key->getAttribute("Sort");
    	    	 $imglibs=$lib_node->getElementsByTagName("img");//读取图文(图片)
    	         foreach ($imglibs as $aimglibs =>$imgkey){
	  	  		       	  $arr[$slib_node]['imgheight']=$imgkey->getAttribute("height");	//图片高度
	  	  		       	  $arr[$slib_node]['imgwidth']=$imgkey->getAttribute("width");    //图片宽度
	  	  		       	  $arr[$slib_node]['imgislink']=$imgkey->getAttribute("islink"); //图片是否链接1是0否
	  	  		       	  $arr[$slib_node]['imgtarget']=$imgkey->getAttribute("target");  //图片链接目标
	  	  		 }
    	    	 $textlibs=$lib_node->getElementsByTagName("text");//读取图文(文字)
	  	  		 foreach ($textlibs as $atextlibs =>$textkey){
	  	  		    $arr[$slib_node]['itextnum']=$textkey->getAttribute("num");//读取文字多少条数
	  	  		    $arr[$slib_node]['itislink']=$textkey->getAttribute("islink");//读取文字多少条数
	  	  		    $arr[$slib_node]['atexttarget']=$textkey->getAttribute("target");//文字链接目标
	  	  		  }
	  	  	      $arr[$slib_node]['dbdata']=$this->get_dynamicDBview($key->getAttribute("Sort"),$this->LibIDC,5,0,$textkey->getAttribute("num"));
    	    }
		  }
	   }
	}
	return $arr;
}
/**
 *   OPENSHOP 管理中心聚集页设定程序
 *   @package     OpenShop
 *   @version     $Id: createdynamic.php,v 1.0 2009/07/29 11:18:17 modified $
 *   @author      杨敬君 <aqkt_436@hotmail.com>
 */
//根据类型不同从数据库获取相应的值
function get_dynamicDB($sort=0,$lib=0,$ty=0,$textnum=0,$atextnum=0){
	$sql = 'select * from ' . $GLOBALS['os']->table('dynamic') . " WHERE dy_sort='$sort' and dy_Project='$lib' and dy_type='$ty'";
	$res = $GLOBALS['db']->getAll($sql);
    $arr = array();
    foreach ($res AS $idx => $row)
    {
            $arr["id"]= $row["dy_id"];
            $arr["aasort"]= $sort;
    	switch ($ty){
    		case 1:
    		     for($i=1;$i<=$textnum;$i++){
    		      $arr["Text$i"]= $row["dy_Text$i"];
    		      $arr["Url$i"]= $row["dy_TextCUrl$i"];
    		      }
    		      $arr["Type"]=$row["dy_Type"];
    	         break;
    		case 2:
    	    	 for($i=1;$i<=$textnum;$i++){
    		      $arr["dy_PPath$i"]= $row["dy_PPath$i"];
    		      $arr["dy_PCUrl$i"]= $row["dy_PCUrl$i"];
    		      }
    		      $arr["Type"]=$row["dy_Type"];
    			break;
    		case 3:
    			  $arr["dy_FPath"]= $row["dy_FPath"];
    			  $arr["Type"]=$row["dy_Type"];
    			  break;
    		case 4:
    			  $arr["dy_Html"]= $row["dy_Html"];
    			  $arr["Type"]=$row["dy_Type"];
    			  break;
    		case 5:
    			  $arr["dy_PPath1"]= $row["dy_PPath1"];
    		      $arr["dy_PCUrl1"]= $row["dy_PCUrl1"];
    	          for($i=1;$i<=$atextnum;$i++){
    		      $arr["Text$i"]= $row["dy_Text$i"];
    		      $arr["Url$i"]= $row["dy_TextCUrl$i"];
    		      }
    		     $arr["Type"]=$row["dy_Type"];
    			break;
    			default:
    				break;
    	}
    }
    return $arr;
}
//循环数据
function get_dynamicDBview($sort=0,$lib=0,$ty=0,$textnum=0,$atextnum=0){
    $zhiduan="dy_id,dy_Sort,dy_Type,";
    switch($ty){
        case 1:
        $zhiduan=$zhiduan."dy_Text1,dy_TextCUrl1,dy_Text2,dy_TextCUrl2,dy_Text3,dy_TextCUrl3,dy_Text4,dy_TextCUrl4,dy_Text5,dy_TextCUrl5,dy_Text6,dy_TextCUrl6";
        break;
        case 2:
        $zhiduan=$zhiduan."dy_PPath1,dy_PCUrl1,dy_PPath2,dy_PCUrl2";
        break;
        case 3:
        $zhiduan=$zhiduan."dy_FPath";
        break;
        case 4:
        $zhiduan=$zhiduan."dy_Html";
        break;
        default:
        $zhiduan=$zhiduan."dy_PCUrl1,dy_PPath1,dy_Text1,dy_TextCUrl1,dy_Text2,dy_TextCUrl2,dy_Text3,dy_TextCUrl3,dy_Text4,dy_TextCUrl4,dy_Text5,dy_TextCUrl5,dy_Text6,dy_TextCUrl6";
        break;
    }
	$sql = "select $zhiduan from " . $GLOBALS['os']->table('dynamic') . " WHERE dy_sort='$sort' and dy_Project='$lib' and dy_type='$ty'";
    ///echo $sql."<BR>";
    //exit;
	$res = $GLOBALS['db']->getAll($sql);
    $arr = array();
    foreach ($res AS $idx => $row)
    {
            $arr["id"]= $row["dy_id"];
            $arr["aasort"]= $sort;
    	switch ($ty){
    		case 1:
                 $arr["content"]="";
    		     for($i=1;$i<=$textnum;$i++){
                 $arr["content"]=$arr["content"]."<a href=".$row["dy_TextCUrl$i"].">".$row["dy_Text$i"]."</a><BR>";
    		      }
                  $arr["Type"]=$row["dy_Type"];
    	         break;
    		case 2:
                 $arr["content"]="";
    	    	 for($i=1;$i<=$textnum;$i++){
                 if($row["dy_PCUrl$i"]<>""){
                   $arr["content"]=$arr["content"]."<a href=".$row["dy_PCUrl$i"]."><img src=".$this->imgname.$row["dy_PPath$i"]."?400,150></a><br>";
			     }else{
                   $arr["content"]=$arr["content"]."<img src=".$this->imgname.$row["dy_PPath$i"]."?517,253><br>";
			     }
    		      }
    		      $arr["Type"]=$row["dy_Type"];
    			break;
    		case 3:
                $arr["content"]="
        <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0>
          <param name=movie value=$row[dy_FPath]>
          <param name=quality value=high>
          <embed src=$row[dy_FPath] quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash></embed>
        </object>";		
         $arr["Type"]=$row["dy_Type"];
    			  break;
    		case 4:
                  $arr["content"]=$row["dy_Html"];
    			  $arr["Type"]=$row["dy_Type"];
    			  break;
    		case 5:
                  $arr["content"]="";
                  if($row[dy_PCUrl1]<>""){
	              $arr["content"]=$arr["content"]."<table width=100%><tr>
                  <td colspan=5>
                  <a href=$row[dy_PCUrl1]><img src=".$this->imgname.$row[dy_PPath1]."?517,253></a>
                  </td>
                  </tr>";
	}else{
		$arr["content"]=$arr["content"]."<table width=100%><tr>
                 <td colspan=5><img src=$this->imgname.$row[dy_PPath1]></td></tr>";
	}
    for($b=1;$b<=$atextnum;$b++){
         	if($row["dy_textcUrl$b"]<>""){
           $arr["content"]=$arr["content"]."<tr>
                            <td class='first-cell' colspan=4>
                            <a href=".$row["dy_textcUrl$b"].">".$row["dy_Text$b"]."</a>
                            </tr>";
         	}else{
            $arr["content"]=$arr["content"]."<tr>
                            <td class='first-cell' colspan=4>".$row["dy_Text$b"]."
                            </tr>";
         	}
          }
           $arr["content"]=$arr["content"]."</table>";
    	   $arr["Type"]=$row["dy_Type"];
    			break;
    			default:
    				break;
    	}
    }
    return $arr;
}

/**
 * 获得类型(中文)
 * @access  public
 * @param   array     get_typestring
 * @return  array
 * @author  杨敬君
 * @CreateTime 2009-7-30 15:54
 */
function get_typestring($type=0){
switch ($type){
		case '1':
			$stringname="文字";
			break;
		case '2':
			$stringname="图片";
			break;
		case '3':
			$stringname="flash";
			break;
		case '4':
			$stringname="html";
			break;
	    default:
		    $stringname="图文";
			break;
	}
	return $stringname;
}
/*
 * 文字
 */
function text($i,$value){
	for($a=1;$a<=$i;$a++){
		if($value["Url$a"]<>""){
	    $result=$result."
	    <a href=".$value["Url$a"].">".$value["Text$a"]."</a><BR>";
		}else{
			$result=$result.$value["Text$a"];
			$result=$result."<BR>";
		}
		}
		return $result;
}
/**
 * 图片
 */
function image($i,$h,$w,$value){
		for($a=1;$a<=$i;$a++){
			if($value["dy_PCUrl$a"]<>""){
	    $result=$result."<a href=".$value["dy_PCUrl$a"]."><img src=".$value["dy_PPath$a"]." width='$w' height='$h'></a><br>";
			}else{
		$result=$result."<img src=".$value["dy_PPath$a"]." width='$w' height='$h'><br>";
			}
		}
		return $result;
}
/**
 * flash
 */
function flash($i,$h,$w,$value){
		for($a=1;$a<=$i;$a++){
	    $result=$result."
        <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0 width=$w height=$h>
          <param name=movie value=$value[dy_FPath]>
          <param name=quality value=high>
          <embed src=$value[dy_FPath] quality=high pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash width=$w height=$h></embed>
        </object>";
		}
		return $result;
}
/**
 * 图文
 *
 */
function imtext($h,$w,$atextnum,$value){
	if($value[dy_PCUrl1]<>""){
	    $result="<table width=100%><tr>
                 <td colspan=5>
                 <a href=$value[dy_PCUrl1]><img src='$value[dy_PPath1]' width='$w' height='$h'></a>
                 </td>
                 </tr>";
	}else{
		$result="<table width=100%><tr>
                 <td colspan=5><img src='$value[dy_PPath1]' width='$w' height='$h'></td></tr>";
	}
    for($b=1;$b<=$atextnum;$b++){
         	if($value["Url$b"]<>""){
           $result=$result."<tr>
                            <td class='first-cell' colspan=4>
                            <a href=".$value["Url$b"].">".$value["Text$b"]."</a>
                            </tr>";
         	}else{
           $result=$result."<tr>
                            <td class='first-cell' colspan=4>".$value["Text$b"]."
                            </tr>";
         	}
          }
          $result=$result."</table>";
		return $result;
}
//FTP上传
function ftpupfile($filename,$type){
    global $filesync;
    $imgfile_name = '';
    if($filesync->sync_type=='nfs'){
    	$imgfile_name = $filesync->upload_image($filename,'cms_img');
    }elseif($filesync->sync_type=='ftp'){
       	$filesync->connect();
    	$filesync->setting('dynamic', 'data/cms_img', FALSE);
	    $affix_data = $filesync->put_up($filename);
	    if ($affix_data !== false)
	    {
	        $imgfile_name = $affix_data['virtual_path'];
	    }
	    $filesync->quit();
    }
    return $imgfile_name;
}
//删除
function Delfile($img_id,$img_file=""){
 global $filesync;
     	if($filesync->sync_type=='nfs'){
			    if (!empty($img_file)){
			        @unlink(SOURCE_PATH . $img_file);
			    }
	    	}elseif($filesync->sync_type=='ftp'){
			    $filesync->connect();
			    @$filesync->drop($img_id, 'dynamic');
			    $filesync->quit();
	    	}
}

/**
 * 判断项目库是否存在
 * @param   string      $lib 项目库名
 * @param   string      $ty 类型
 * @param   string      $sort 排序
 * @return  boolean
 */
function is_dyOnly($lib,$ty,$sort)
{
    $res = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('dynamic') .
                                  " WHERE dy_Project = '$lib' and dy_Type='$ty' and dy_Sort='$sort'");
    return $res;
}

}
?>